package service

import (
	"errors"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/admin/service/dto"
	"tuowei.com/common/actions"
	cDto "tuowei.com/common/dto"
	"tuowei.com/pkg/xlog"
)

type SysLiveCourse struct {
	service.Service
}

// GetPage 获取SysLiveCourse列表
func (e *SysLiveCourse) GetPage(c *dto.SysLiveCourseGetPageReq, p *actions.DataPermission, list *[]models.SysLiveCourse, count *int64) error {
	var err error
	var data models.SysLiveCourse

	err = e.Orm.Debug().Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

// Insert 创建SysLiveCourse对象
func (e *SysLiveCourse) Insert(c *dto.SysLiveCourseInsertReq) (int, error) {
	var err error
	var data models.SysLiveCourse
	//var i int64
	//err = e.Orm.Model(&data).
	//	Where("serial_num = ?", c.SerialNum).Count(&i).Error
	//if err != nil {
	//	e.Log.Errorf("db error: %s", err)
	//	return err
	//}
	//if i > 0 {
	//	err := errors.New("课程编号已存在！")
	//	e.Log.Errorf("db error: %s", err)
	//	return err
	//}

	c.Generate(&data)

	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return 0, err
	}

	return data.Id, nil
}

func (e *SysLiveCourse) UpdateStatus(c *dto.SysLiveCourseUpdateStatusReq) error {
	var err error
	var model = models.SysLiveCourse{}
	e.Orm.First(&model, c.GetId())
	c.Generate(&model)
	db := e.Orm.Save(&model)
	err = db.Error
	if err != nil {
		e.Log.Errorf("Service UpdateStatus error:%s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")

	}
	return nil
}

func (e *SysLiveCourse) Update(c *dto.SysLiveCourseUpdateReq) error {
	var err error
	var model = models.SysLiveCourse{}
	e.Orm.First(&model, c.GetId())
	c.Generate(&model)
	db := e.Orm.Save(&model)
	err = db.Error
	if err != nil {
		xlog.Errorf("Service UpdateStatus error:%s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")

	}
	return nil
}

// Remove 删除SysClass
func (e *SysLiveCourse) Remove(c *dto.SysLiveCourseById, p *actions.DataPermission) error {
	var err error
	var data models.SysLiveCourse

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, c.GetId())
	if err = db.Error; err != nil {
		xlog.Errorf("Error found in  RemoveSysLiveCourse : %s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

// Get 获取SysLiveCourse对象
func (e *SysLiveCourse) Get(d *dto.SysLiveCourseById, p *actions.DataPermission, model *models.SysLiveCourse) error {
	var data models.SysLiveCourse
	var err error
	err = e.Orm.Model(&data).Debug().
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		xlog.Errorf("db error: %s", err)
		return err
	}

	if err != nil {
		xlog.Errorf("db error: %s", err)
		return err
	}

	return nil
}

func (e *SysLiveCourse) GetBindingCourseList(list *[]models.CourseList) error {
	var model models.SysLiveCourse
	return e.Orm.Debug().Model(model).Where("is_bind_offline = ?", 1).Find(&list).Error
}
